/**
 * @ author: 汪孟苹
 */
define(["jquery","bbcare","fu/app/comm/validate","fu/app/comm/attention","oms_config/root",
	"scprit/followup/app/patientManager/addPatient.servers.js",
	"scprit/followup/app/patientManager/addPatient.ui.js",
	"scprit/followup/app/question/question.ui.js",
	"scprit/followup/app/question/question.servers.js",
	"scprit/followup/app/question/question.event.js"
	],function($,$bb,$validate,$attention,$root,$addPatientServers,$addPatientUi,$questionUi,$questionServers,$questionEvent){
	var event = {};
	/**
	 * 点击关闭，跳转到工作站
	 */
	var onCloseSelect = function(){
		var config = $root.init();
		$bb.selectNavbar(config.route.addPatientClose);
	}
	event.onCloseSelect = function(){
		onCloseSelect();
	}
	/**
	 * 根据患者基本信息跳转到门诊录入页面
	 */
	var getPatientInfoCallback =  function(data){
		var imgSrc = 'page/followup/imgs/comm/' + (data.PatientInfo.gender == 'F'? 'girl':'boy') + '.png';
		var params = {
			patientSchemeId: data.QueueSchemeInfo.ptSchemeId,
			followQueueId: data.QueueSchemeInfo.queueId,
			patientName: data.PatientInfo.userName,
			imgSrc: imgSrc,
			queuename: data.QueueSchemeInfo.queueName,
			patientId: data.PatientInfo.id,
		};
		var config = $root.init();
		$bb.openPage(config.route.addPatientJump,params);
	}
	/**
	 * 保存成功后的回调函数
	 */
	var getSaveBuildCallback =  function(data){
		$bb.toastShow("添加成功！",2000);
		$addPatientServers.ajaxPatientInfo(data,getPatientInfoCallback);	
	}
	/**
	 * 点击保存按钮
	 */
	var onSaveClick = function(params){
		var patientName = $("#questionDetail form").find("input[name='Pub1100002']").val();
        var formId = $("#questionDetail form").attr("id");
        var questiones = $questionServers.getDataForDom(formId);
        $addPatientServers.ajaxSaveBuildQuestions(params,patientName,questiones,getSaveBuildCallback);	
	}
	/**
	 * 表单提交前的校验
	 */
	var onValidateClick = function(){
		var params = $(this).data("params");
		if( $validate.init() ){
		    onSaveClick(params);         	
		}
	}
	/**
	 * 建档问卷请求成功会后的回调函数
	 */
	var getBuildQuestCallback = function(data,params,values){
		$questionUi.init({
            id: "questionDetail",
            list: data.questiones,
            showIcon: true,
            values: values,
        });
        $questionEvent.init({
	        "getPatientsByPhone":{
	        	params:params,
	        	callback:buildQuestions
	        },
	        "getGeneFusion":{
				"followQueueId":params.queueId,
				"values":values
			}
        });
        $attention.init();
        $('.validatebox').data("attentionFun",$attention.blurInit);	// 校验之前提示警告
		$validate.blurInit();
        var config = $root.init();
        var topbars = [
			{
				"icon":"fa fa-floppy-o",
				"name":"保存",
				"callback":onValidateClick,
				"params":params
			}
		];
		if( config.dom.addPatientClose ){
			topbars.push({
					"icon":"fa fa-times",
					"name":"关闭",
					"callback":onCloseSelect,
			});
		}
		$bb.toolbar(topbars);
	}
	/**
	 * 建档问卷
	 */
	var buildQuestions = function(params,result){
		$addPatientServers.ajaxBuildQuestions(params,getBuildQuestCallback,result.values);
	}
	/**
	 * 建档问卷
	 */
	var bookbuilding = function(params){
		$addPatientUi.tit({
			id: "queueName",
			queueName: params.queueName
		});
		buildQuestions(params,{"values":null});
	}
	event.bookbuilding = function(params){
		bookbuilding(params);
	}
	/**
	 * 点击队列card
	 */
	event.onQueueNavSelect = function(){
		$(this).addClass("active").siblings().removeClass("active");
		var queueName = $(this).find(".queue-name").text();
		var queueId = $(this).attr("queueId");
		var schemeId = $(this).attr("schemeId");
		var params = {
			"queueName":queueName,
			"queueId":queueId,
			"schemeId":schemeId
		};
		bookbuilding(params);
	}
    
	return event;
});